import 'dart:convert';
import 'dart:io';

void listener([int port = 10004]) async {
  RawDatagramSocket socket =
      await RawDatagramSocket.bind(InternetAddress.anyIPv6, port);
  // socket.readEventsEnabled=false;
  socket.listen((RawSocketEvent event) {
    switch (event) {
      case RawSocketEvent.read:
        var dg = socket.receive();
        print("host:${dg!.address.host}");
        print("addr:${dg.address.address}");
        print("port:${dg.port}");
        print("--------");
        print(utf8.decode(dg.data));
        print("------------------------------");
        print(socket.writeEventsEnabled);
        socket.writeEventsEnabled = true;
        break;
      case RawSocketEvent.write:
        print("[write]");
        break;
      case RawSocketEvent.readClosed:
        print("readClosed");
        socket.close();
        break;
      case RawSocketEvent.closed:
        print("closed");
        break;
      default:
        throw "Unexpected event $event";
    }
  });
}

void main(List<String> args) {
  listener();
}
